package swingtest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

import javax.swing.JPanel;

public class J_Panel extends JPanel {
	private Vector<Vector<Point>> m_vectorSet = new Vector<>();

	public J_Panel() {
		// addMouseListener(new MouseListener() {
		//
		// @Override
		// public void mouseReleased(MouseEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void mousePressed(MouseEvent e) {
		// // TODO Auto-generated method stub
		// Point p = new Point(e.getX(),e.getY());
		// Vector<Point> v = new Vector<>();
		// v.add(p);
		// m_vectorSet.add(v);
		// }
		//
		// @Override
		// public void mouseExited(MouseEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void mouseEntered(MouseEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void mouseClicked(MouseEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		// });
		//
		// addMouseMotionListener(new MouseMotionListener() {
		//
		// @Override
		// public void mouseMoved(MouseEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void mouseDragged(MouseEvent e) {
		// // TODO Auto-generated method stub
		// Point p = new Point(e.getX(), e.getY());
		// int n = m_vectorSet.size() - 1;
		// Vector<Point> v = m_vectorSet.get(n);
		// v.add(p);
		// repaint();
		// }
		// });
		//
		//
		addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				Point p = new Point(e.getX(), e.getY());
				Vector<Point> v = new Vector<>();
				v.add(p);
				m_vectorSet.add(v);
			}
		});
		addMouseMotionListener(new MouseMotionAdapter() {

			@Override
			public void mouseDragged(MouseEvent e) {
				Point p = new Point(e.getX(), e.getY());
				int n = m_vectorSet.size() - 1;
				Vector<Point> v = m_vectorSet.get(n);
				v.add(p);
				repaint();
			}

		});
	}

	protected void paintComponent(Graphics g) {
		g.clearRect(0, 0, getWidth(), getHeight());
		Vector<Point> v;
		Point s, t;
		int i, j, m;
		int n = m_vectorSet.size();
		for (i = 0; i < n; i++) {
			v = m_vectorSet.get(i);
			m = v.size() - 1;
			for (j = 0; j < m; j++) {
				s = (Point) v.get(j);
				t = (Point) v.get(j + 1);
				g.setColor(Color.green);
				g.drawLine(s.x, s.y, t.x, t.y);
			}
		}
	}

	public Dimension getPreferredSize() {
		return new Dimension(250, 120);
	}

}
